//
//  DetailedFirstViewController.m
//  NBTripe
//
//  Created by 陈曦 on 16/1/9.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "DetailedFirstViewController.h"
#import "TravelPlaceViewController.h"
#import "BoutiqueTravelNotesViewController.h"
#import "TitleLabelTableViewCell.h"
#import "TjianTableViewCell.h"
#import "ALLDiDianTableViewCell.h"
#import "VCWebViewController.h"
#import "VCPhotoViewController.h"

@interface DetailedFirstViewController ()<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic,strong)UIImageView *headImageView;//头部图片
@property(nonatomic,strong)UITableView *tableView;//列表
@property (nonatomic,strong) DetailedFirstModel *detailedModel;
@property (nonatomic,strong) NSString *str;

@end

@implementation DetailedFirstViewController
//静态变量 (整个类都可以使用) const(变量值不可以改变) volatile 会发的,易变的(变量值随时可以改变)
static CGFloat kImageOriginHight = 200;
- (void)viewDidLoad {
    [super viewDidLoad];
    [self getDataSource];
    
    
    
    _headImageView= [[UIImageView alloc]init];
    _headImageView.frame=CGRectMake(0, -kImageOriginHight,self.view.width,kImageOriginHight);
    _headImageView.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height - 112*XHIGHT) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    _tableView.contentInset=UIEdgeInsetsMake(kImageOriginHight,0,0,0);
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[ToolsTableViewCell class] forCellReuseIdentifier:@"ToolsTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TitleLabelTableViewCell" bundle:nil] forCellReuseIdentifier:@"TitleLabelTableViewCell"];
    [self.tableView registerClass:[TjianTableViewCell class] forCellReuseIdentifier:@"TjianTableViewCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"ALLDiDianTableViewCell" bundle:nil] forCellReuseIdentifier:@"ALLDiDianTableViewCell"];
    [self.tableView addSubview: _headImageView];
    [self.view addSubview:self.tableView];
    // Do any additional setup after loading the view.
}

#pragma mark - TableView


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 4;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 100;
    }else if (indexPath.row == 1) {
        return 50;
    }else if (indexPath.row == 2) {
        return  (self.view.width/ 2 - 8 ) *(self.detailedModel.hottest_sitesArray.count / 2 +_detailedModel.hottest_sitesArray.count % 2);
    }else if (indexPath.row == 3) {
        return 50;
    }
    return 100;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ToolsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ToolsTableViewCell"];
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.detaiedModel = self.detailedModel;
        cell.toolsTiao = ^(NSString *url) {
            WebViewController *web = [[WebViewController alloc] init];
         web.url = url;
             //       [web WebUrl:url];
            [self.navigationController pushViewController:web animated:YES];
//            [self presentViewController:web animated:YES completion:nil];
            
            
        };
        cell.toolsDiDian = ^(DetailedFirstModel *detail) {
            TravelPlaceViewController *traveVC = [[TravelPlaceViewController alloc] init];
            traveVC.detailedModel = detail;
            [self.navigationController pushViewController:traveVC animated:YES];
        };
        cell.toolsJP = ^(NSString *url) {
            BoutiqueTravelNotesViewController *btnVC = [[BoutiqueTravelNotesViewController alloc] init];
            btnVC.urls = url;
            [self.navigationController pushViewController:btnVC animated:YES];
            
        };
        
        return cell;

    }else if (indexPath.row == 1) {
        TitleLabelTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TitleLabelTableViewCell"];
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }else if (indexPath.row == 2) {
        TjianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TjianTableViewCell"];
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        cell.detailModel = self.detailedModel;
        cell.webGo = ^(NSString *url) {
            WebViewController *web = [[WebViewController alloc] init];
            web.url = url;
            [self.navigationController pushViewController:web animated:YES];
        };
        return cell;
    }else if (indexPath.row == 3) {
        ALLDiDianTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ALLDiDianTableViewCell"];
         [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }
    
    UITableViewCell *cell = [[UITableViewCell alloc] init];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 3) {
       
        TravelPlaceViewController *traveVC = [[TravelPlaceViewController alloc] init];
        traveVC.detailedModel = self.detailedModel;
        [self.navigationController pushViewController:traveVC animated:YES];
    }
}

#pragma mark -- 滚动视图的代理方法
- (void)scrollViewDidScroll:(UIScrollView*)scrollView{
    /**
     *  关键处理：通过滚动视图获取到滚动偏移量从而去改变图片的变化
     */
    //获取滚动视图y值的偏移量
    CGFloat yOffset  = scrollView.contentOffset.y;
    CGFloat xOffset = (yOffset +kImageOriginHight)/2;
    
    if(yOffset < -kImageOriginHight) {
        CGRect f = self.headImageView.frame;
        f.origin.y= yOffset;
        f.size.height=  -yOffset;
        f.origin.x= xOffset;
        //int abs(int i); // 处理int类型的取绝对值
        //double fabs(double i); //处理double类型的取绝对值
        //float fabsf(float i); //处理float类型的取绝对值
        f.size.width=self.view.width + fabs(xOffset)*2;
        
        self.headImageView.frame= f;
    }
    if (yOffset < -250) {
        VCPhotoViewController *photo = [[VCPhotoViewController alloc] init];
        photo.str = self.str;
        [self.navigationController pushViewController:photo animated:YES];
    }
    
    
}

- (void)getDataSource {
    self.str = [NSString stringWithFormat:@"%@/%@/%@", destinationNextURL, self.elementModel.type, self.elementModel.nId];
    [XYNetWorkTool getWithUrl:self.str parameter:nil httpHeader:[NSDictionary dictionaryWithObjectsAndKeys:@"BreadTrip/6.2.0/zh (iPhone7,1; iPhone OS9.2; zh-Hans-CN zh_CN) Paros/3.2.13",@"User-Agent", nil] responseType:ResponseTypeJSON success:^(id result) {
        self.detailedModel = [[DetailedFirstModel alloc] initWithDataSource:result];
        [_headImageView sd_setImageWithURL:[NSURL URLWithString:[self.detailedModel.hottest_placesArray[0] photo]]];
        [self.tableView reloadData];
        
    } fail:^(NSError *error) {
        NSLog(@"目的地的首页数据获取失败，错误为%@", error);
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
